<template>
  <div class="main">
    <el-card class="box-card">
      <Form ref="form" :formobj="formobj" :formdata="formData" :formrules="formrules" @changelanmu="changelanmu" />
      <div style="padding-left:100px">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-card>
  </div>
</template>
<script>
import Form from '@/components/form.vue'
import { contentApiAdd, columnqueryAll, contentApiedit, contentApiqueryOne } from '@/api/webpage'
import { validateNum } from '@/utils/validator'
export default {
  name: 'Addform',
  components: {
    Form
  },
  data() {
    return {
      options: [],
      formData: {
        columnId: '',
        ctitle: '',
        title: '',
        no: '',
        content: '',
        url: '',
        fileUrl: '',
        signBy: '',
        createTime: '',
        type: 'article'
      },
      formobj: [
        { label: '所属栏目', key: 'columnId', type: 'select', options: [] },
        { label: '发布时间', key: 'createTime', type: 'date' },
        /* { label: "封面", key: "url", type: "pic" },*/
        { label: '标题', key: 'title', type: 'input' },
        { label: '作者', key: 'author', type: 'input' },
        { label: '审核人', key: 'postedBy', type: 'input' },
        /* { label: "排序", key: "no", type: "input" },*/
        { label: '附件', key: 'fileUrl', type: 'execl', word: '请上传文件' },
        { label: '内容', key: 'content', type: 'editor' }
      ],
      formrules: {
        columnId: [
          { required: true, message: '请选择栏目', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        author: [
          { required: true, message: '请输入作者', trigger: 'blur' }
        ],
        /* url: [
                    { required: true, message: "请上传封面", trigger: "blur" },
                ],*/
        no: [
          { required: false, message: '请输入序号', trigger: 'blur' },
          {
            validator: validateNum,
            message: '请输入正整数',
            trigger: 'blur'
          }
        ],
        content: [
          { required: false, message: '请输入正文', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    if (this.$route.query.cid) { // 如果是点击栏目进来的  就需要禁用选择
      this.formData.columnId = this.$route.query.cid
      this.changeForm(this.$route.query.cid)
      this.formobj[0].disabled = true
    } else {
      this.getlist()
      this.formobj[0].disabled = false
    }

    if (this.$route.query.id) {
      this.getdata(this.$route.query.id)
    }
  },
  methods: {
    changeForm(val) {
      if (val == 'A6DA25D541C140B3AE4341A21A347320' || val == '1817DA29BBB047B7ADBE6F6D1EC65301') { // 精彩视频
        this.formobj = [
          { label: '所属栏目', key: 'columnId', type: 'select', options: [] },
          { label: '标题', key: 'title', type: 'input' },
          { label: '封面', key: 'url', type: 'pic' },
          { label: '作者', key: 'author', type: 'input' },
          { label: '审核人', key: 'postedBy', type: 'input' },
          { label: '视频', key: 'fileUrl', type: 'video', word: '请上传mp4文件' },
          { label: '内容', key: 'content', type: 'editor' }
        ]
        this.formData = {
          columnId: val,
          title: '',
          fileUrl: '',
          url: '',
          author: ''
        }
        this.formrules = {
          title: [
            { required: true, message: '请输入标题', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '请上传封面', trigger: 'blur' }
          ],
          author: [
            { required: true, message: '请输入作者', trigger: 'blur' }
          ],
          no: [
            { required: false, message: '请输入序号', trigger: 'blur' },
            {
              validator: validateNum,
              message: '请输入正整数',
              trigger: 'blur'
            }
          ],
          content: [
            { required: false, message: '请输入正文', trigger: 'change' }
          ]
        }
      }
      /* else if (val == 'BD11A0CD7C834F318D8664DB59F2391E') { // 工作简报
                // this.formobj[1]= { label: "附件", key: "fileUrl", type: "execl",word:"请上传文件" }
            } */
      else if (val == '3B0045D69BEB478E9347BB049B2014E1' || val == 'BD11A0CD7C834F318D8664DB59F2391E' ||
              val == '73AC250B8E0A4C92A030D954A49405B5') { // 通知通告 工作简报 大事记 考勤情况
        this.formobj = [
          { label: '所属栏目', key: 'columnId', type: 'select', options: [] },
          { label: '标题', key: 'title', type: 'input' },
          { label: '作者', key: 'author', type: 'input' },
          { label: '审核人', key: 'postedBy', type: 'input' },
          { label: '发布时间', key: 'createTime', type: 'date' },
          { label: '附件', key: 'fileUrl', type: 'execl', word: '请上传文件' }
          /*  { label: "签发人", key: "signBy", type: "input" },*/

          /* { label: "内容", key: "content", type: "editor" }*/
        ]
        this.formrules = {
          fileUrl: [
            { required: true, message: '请上传附件', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '请输入标题', trigger: 'blur' }
          ],
          no: [
            { required: false, message: '请输入序号', trigger: 'blur' },
            {
              validator: validateNum,
              message: '请输入正整数',
              trigger: 'blur'
            }
          ],
          signBy: [
            { required: true, message: '请输入', trigger: 'blur' }
          ],
          postedBy: [
            { required: true, message: '请输入', trigger: 'blur' }
          ]
        }
      } else if (
        val == 'D28CDA7DAAB04160BC55CDA3603440F5' ||
                val == '356C9A236C864596AEB827720BDA5507' ||
                val == '0856B89F3A0E4B938FA546E793FC718A' ||
                val == '21DC162B37E34D61954B13AA2C4CCF9D' ||
                val == '1C4023D89C8841C4B58F856C89166B89' ||
                val == '1A0E43D07B694D288FBCC9A89632411D' ||
                val == '53B930B4A1354AD3801E8E4EEEE02381' ||
                val == 'DF8A0370E48045BA8A2CC27258CA4418' ||
                val == '5F14BDA77EB449F385B8CE0478E9C88B' ||
                val == 'ABF991AB428948CBAA8C4103C6A1C954') {
        this.formobj = [
          { label: '所属栏目', key: 'columnId', type: 'select', options: [] },
          { label: '发布时间', key: 'createTime', type: 'date' },
          { label: '标题', key: 'title', type: 'input' },
          { label: '作者', key: 'author', type: 'input' },
          { label: '审核人', key: 'postedBy', type: 'input' },
          /* { label: "排序", key: "no", type: "input" },*/
          { label: '附件', key: 'fileUrl', type: 'execl', word: '请上传文件' },
          { label: '内容', key: 'content', type: 'editor' }
        ]
        this.formData = {
          columnId: val,
          title: '',
          fileUrl: '',
          url: ''
        }
      } else if (val == '4BF37E6D3E194FD8879F7ED7B4B006BE') { // 机关动态
        this.formData = {
          columnId: val,
          ctitle: '',
          title: '',
          no: '',
          content: '',
          url: '',
          fileUrl: '',
          signBy: '',
          createTime: '',
          type: 'article',
          articleType: ''
        }
        this.formobj = [
          { label: '所属栏目', key: 'columnId', type: 'select', options: [] },
          {
            label: '新闻类型', key: 'articleType', type: 'select', options: [
              { label: '防火工作', value: '防火工作' },
              { label: '救援工作', value: '救援工作' },
              { label: '政治工作', value: '政治工作' },
              { label: '后勤工作', value: '后勤工作' },
              { label: '纪检工作', value: '纪检工作' },
              { label: '综合工作', value: '综合工作' }
            ]
          },
          /* { label: "封面", key: "url", type: "pic" },*/
          { label: '标题', key: 'title', type: 'input' },
          { label: '作者', key: 'author', type: 'input' },
          { label: '审核人', key: 'postedBy', type: 'input' },
          /* { label: "排序", key: "no", type: "input" },*/
          { label: '内容', key: 'content', type: 'editor' },
          { label: '附件', key: 'fileUrl', type: 'execl', word: '请上传文件' },
          { label: '发布时间', key: 'createTime', type: 'date' }
        ]
        this.formrules = {
          articleType: [
            { required: true, message: '请选择新闻类型', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '请输入标题', trigger: 'blur' }
          ],
          /* url: [
                        { required: true, message: "请上传封面", trigger: "blur" },
                    ],*/
          no: [
            { required: false, message: '请输入序号', trigger: 'blur' },
            {
              validator: validateNum,
              message: '请输入正整数',
              trigger: 'blur'
            }
          ],
          content: [
            { required: false, message: '请输入正文', trigger: 'change' }
          ]

        }
      } else if (val == '0E1E9FAD3D9F40AEB4CAF8D9701B8C8B') { // 领导批示
        this.formobj = [
          { label: '所属栏目', key: 'columnId', type: 'select', options: [] },
          /* { label: "封面", key: "url", type: "pic" },*/
          { label: '发布时间', key: 'createTime', type: 'date' },
          { label: '标题', key: 'title', type: 'input' },
          { label: '作者', key: 'author', type: 'input' },
          { label: '审核人', key: 'postedBy', type: 'input' },
          { label: '附件', key: 'fileUrl', type: 'execl', word: '请上传文件' }
        ]
        this.formData = {
          columnId: val,
          title: '',
          fileUrl: '',
          url: ''
        }
      }else if(val == 'FE9D74C4870047468AC6F8267C5E1481'){
        this.formobj = [
          { label: '所属栏目', key: 'columnId', type: 'select', options: [] },
          { label: '标题', key: 'title', type: 'input' },
          { label: '发布时间', key: 'createTime', type: 'date' },
          { label: '附件', key: 'fileUrl', type: 'execl', word: '请上传文件' }
          /*  { label: "签发人", key: "signBy", type: "input" },*/

          /* { label: "内容", key: "content", type: "editor" }*/
        ]
        this.formrules = {
          fileUrl: [
            { required: true, message: '请上传附件', trigger: 'blur' }
          ],
        }
      }
      this.getlist()
    },
    changelanmu(val) {
      this.changeForm(val)
    },
    getdata(id) {
      contentApiqueryOne({ id: id }).then(res => {
        if (res.code == 0) {
          this.formData = res.data
        }
        // 设置新闻栏目
        // this.changeForm(this.formData.columnId)
        console.log('新闻数据：', this.formData)
      })
    },
    getlist() {
      this.options = []
      this.formobj[0].options = []
      const data = {
        name: '',
        code: '',
        parentId: ''
      }
      columnqueryAll(data).then((res) => {
        if (res.code == 0) {
          if (this.$route.query.cid) {
            res.data.map(item => {
              this.options.push({ label: item.name, value: item.id })
              this.formobj[0].options.push({ label: item.name, value: item.id })
            })
          } else {
            res.data.map(item => {
              if (item.name != '友情链接' && item.name != '审核管理' &&
                    item.name != '精彩视频' &&
                    item.name != '视频点播' &&
                    // 政策法规 不包含政策法规栏目
                    item.name.indexOf('政策法规') == -1 &&
                    item.name != '轮播新闻' &&
                    item.name != '图文链接管理' &&
                    item.name != '今日排班' &&
                    item.name != '排版模板' &&
                    item.name != '下载中心' &&
                    item.name != '精彩瞬间' &&
                    item.name != '值班人员' &&
                    item.name != '办公入口' &&
                    item.name != '各地动态' &&
                    // item.name != '机关动态' &&
                    item.name != '支队概况' &&
                    // item.name != '防火工作' &&
                    // item.name != '救援工作' &&
                    // item.name != '政治工作' &&
                    // item.name != '后勤工作' &&
                    // item.name != '纪检工作' &&
                    // item.name != '综合工作' &&
                    item.name != '法规一' &&
                    item.name != '法规二' &&
                    item.name != '视频插件' &&
                    item.name != '省内消防系统' &&
                    item.name != '常用字体' &&
                    item.name != '安全杀毒' &&
                    item.name != '消防站训练计划' &&
                    item.name != '安全杀毒吉林省消防救援总队' &&
                    item.name != '国家消防救援局' &&
                    item.name != '吉林省消防救援总队' &&
                    item.name != '办公软件' &&
                    item.name != '统计信息' &&
                    item.name != '考勤情况' &&
                    item.name != '值班日记' &&
                    item.name != '通讯录') {
                //  验证各地动态栏目
                if (
                  item.id == '437C9EAB57FB4C558A33EC96445F7487' ||
                      item.id == '21BCBF2C36DF4175A22296F3EE3D069F' ||
                      item.id == '6AEBF34D0F9D44D6A769A75D9CBB0F50' ||
                      item.id == '94D8ADCCF64640E9B986A38380B9DAA9' ||
                      item.id == '52BB3EDFD6FA47A891191F2B43FE643D' ||
                      item.id == '45F7E806AF264307BC0F823EB1657919'
                ) {
                  item.name = '各地动态—' + item.name
                }
                this.options.push({ label: item.name, value: item.id })
                this.formobj[0].options.push({ label: item.name, value: item.id })
              }
            })
          }
        }
      })
    },
    handleCurrentChange(val) {
      this.params.page = val
    },
    submit() {
      this.$refs.form.getData().then(res => {
        // return console.log("addform数据",res)
        if (Array.isArray(res.fileUrl)) {
          let fileurl = []
          let filename = []
          fileurl = res.fileUrl.map(item => {
            console.log(item)
            if (item.response) {
              if (item.response.data[0].address.includes('\\')) {
                return item.response.data[0].address.replace('\\', '/')
              } else {
                return item.response.data[0].address
              }
            } else {
              return item.url
            }
          })
          filename = res.fileUrl.map(item => {
            return item.name
          })
          res.fileUrl = fileurl.join(',')
          res.fileName = filename.join(',')
        }
        // 验证是增加还是编辑
        if (this.$route.query.type == 'edit') {
          contentApiedit(res).then((response) => {
            if (response.data == 1) {
              this.$message.success('成功')
              this.$router.go(-1)
            } else {
              this.$message.error('失败')
            }
          })
            .catch((err) => {
              console.log(err)
            })
        } else {
          res.type = 'article'
          console.log('添加新闻数据是：', res)
          let colId = res.columnId
          if(colId == 'FE9D74C4870047468AC6F8267C5E1481'){ // 考勤记录无需审核 直接通过
            res.approval = '1'
          }
          contentApiAdd(res).then((response) => {
            if (response.data == 1) {
              this.$message.success('成功')
              if(colId == 'FE9D74C4870047468AC6F8267C5E1481'){ // 考勤记录无需审核 直接跳转到之前页面
                this.$router.go(-1)
              }else{
                this.$router.push('/dashboard/index')
              }
            } else {
              this.$message.error('失败')
            }
          })
            .catch((err) => {
              console.log(err)
            })
        }
      })
    },
    cancel() {
      this.$router.go(-1)
    }
  }
}
</script>
<style scoped>@import "~@/assets/css/adminstyle.css";</style>
